package cn.online.taxi.manager.service.service.impl;

import cn.online.taxi.common.exception.OnlineTaxiException;
import cn.online.taxi.manager.dto.DtoUtil;
import cn.online.taxi.manager.dto.front.CarInfo;
import cn.online.taxi.manager.mapper.TbCarInfoMapper;
import cn.online.taxi.manager.pojo.TbCarInfo;
import cn.online.taxi.manager.pojo.TbCarInfoExample;
import cn.online.taxi.manager.service.service.CarMangerService;
import com.alibaba.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author BinBini
 * @Date 2021/4/26 10:33
 * @Version 1.0
 */
@Service
public class CarManagerServiceImpl implements CarMangerService {

    @Resource
    private TbCarInfoMapper tbCarInfoMapper;

    @Override
    public TbCarInfo checkCarById(Integer carId) {
        return tbCarInfoMapper.selectByPrimaryKey(carId);
    }

    @Override
    public boolean checkData(String plateNumber) {
        boolean result = true;
        TbCarInfoExample example = new TbCarInfoExample();
        TbCarInfoExample.Criteria criteria = example.createCriteria();
        criteria.andPlateNumberEqualTo(plateNumber);
        List<TbCarInfo> list = tbCarInfoMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            result = false;
        }
        return result;
    }

    @Override
    public int uploadCarInfo(CarInfo carInfo) {
        if(carInfo.getPlateNumber().isEmpty() || carInfo.getCarImg().isEmpty() || carInfo.getCarTypeId() == null || carInfo.getRegistDate() == null) {
            return -1;      // 车辆信息不正确
        }
        boolean result = checkData(carInfo.getPlateNumber());
        if (!result) {
            return 0;       // 车辆已被注册
        }
        TbCarInfo tbCarInfo = DtoUtil.CarInfoToTbCarInfo(carInfo);
        // TODO 根据城市名获取城市代码
        tbCarInfo.setOperationStatus(1);
        tbCarInfo.setPublishTime(new Date());
        tbCarInfo.setUseStatus(1);
        tbCarInfo.setCreateTime(new Date());
        tbCarInfo.setUpdateTime(new Date());

        if(tbCarInfoMapper.insert(tbCarInfo) != 1) {
            throw new OnlineTaxiException("上传车辆失败");
        }
        return 1;
    }
}
